<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="redis," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="hiredishiredis是redis数据库的C接口，支持 Redis 完整的命令集、管线以及事件驱动编程。目前只能在Linux环境下使用，只需要几个基本的函数就可以操作redis数据库。使用参考">
<meta property="og:type" content="article">
<meta property="og:title" content="Hiredis">
<meta property="og:url" content="https://my.oschina.net/u/588736/blog/2015/10/15/hiredis_abc/index.html">
<meta property="og:site_name" content="晚来风">
<meta property="og:description" content="hiredishiredis是redis数据库的C接口，支持 Redis 完整的命令集、管线以及事件驱动编程。目前只能在Linux环境下使用，只需要几个基本的函数就可以操作redis数据库。使用参考">
<meta property="og:updated_time" content="2016-11-28T09:56:24.956Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hiredis">
<meta name="twitter:description" content="hiredishiredis是redis数据库的C接口，支持 Redis 完整的命令集、管线以及事件驱动编程。目前只能在Linux环境下使用，只需要几个基本的函数就可以操作redis数据库。使用参考">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="https://my.oschina.net/u/588736/blog/2015/10/15/hiredis_abc/"/>


  <title> Hiredis | 晚来风 </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">晚来风</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">林花谢了春红，太匆匆。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Hiredis
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2015-10-15T00:00:00+08:00" content="2015-10-15">
              2015-10-15
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/数据库/" itemprop="url" rel="index">
                    <span itemprop="name">数据库</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="hiredis"><a href="#hiredis" class="headerlink" title="hiredis"></a><a href="https://redislabs.com/hiredis" target="_blank" rel="external">hiredis</a></h1><p>hiredis是redis数据库的C接口，支持 Redis 完整的命令集、管线以及事件驱动编程。目前只能在Linux环境下使用，只需要几个基本的函数就可以操作redis数据库。<a href="https://redislabs.com/redis-howto#using-redis-with-c" target="_blank" rel="external">使用参考</a><br><a id="more"></a></p>
<h2 id="C使用redis"><a href="#C使用redis" class="headerlink" title="C使用redis"></a>C使用redis</h2><p>C连接使用redis，需要一个C Redis客户端  </p>
<h3 id="函数说明："><a href="#函数说明：" class="headerlink" title="函数说明："></a>函数说明：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redisContext* redisConnect(const char *ip, int port)  </div><div class="line">说明：该函数用来连接redis数据库，参数为数据库的ip地址和端口，一般redis数据库的端口为6379。  </div><div class="line">类似的提供了一个函数redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv)  </div><div class="line"></div><div class="line">void *redisCommand(redisContext *c, const char *format, ...);</div><div class="line">说明：该函数执行redis数据库中的操作命令，第一个参数为连接数据库时返回的redisContext，剩下的参数为变参，就如C标准函数printf函数一样的变参。  </div><div class="line">返回值为void*，一般强制转换成为redisReply类型，以便做进行进一步的处理。</div><div class="line"></div><div class="line">void freeReplyObject(void *reply);</div><div class="line">说明：释放redisCommand执行后返回的redisReply所占用的内存。  </div><div class="line"></div><div class="line">void redisFree(redisContext *c);</div><div class="line">说明：释放redisConnect()所产生的连接。</div></pre></td></tr></table></figure>
<h3 id="使用步骤"><a href="#使用步骤" class="headerlink" title="使用步骤"></a>使用步骤</h3><p><strong>1.安装hiredis</strong><br>从<a href="https://github.com/redis/hiredis/releases" target="_blank" rel="external">github</a>下载最新的hiredis。</p>
<p><strong>2.使用hiredis打开一个连接到redis</strong><br>要建立一个redis数据库的连接，需要：</p>
<ul>
<li>Redis数据库所在的主机名称或IP地址</li>
<li>该Redis的服务器在侦听的端口号</li>
<li>数据库密码</li>
<li>SSL证书</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"hiredis.h"</span></span></div><div class="line"></div><div class="line">redisContext *c = redisConnect(<span class="string">"hostname"</span>, port);</div><div class="line"><span class="keyword">if</span> (c != <span class="literal">NULL</span> &amp;&amp; c-&gt;err) &#123;</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"Error: %s\n"</span>, c-&gt;errstr);</div><div class="line">    <span class="comment">// handle error</span></div><div class="line">&#125; <span class="keyword">else</span> &#123;</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"Connected to Redis\n"</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line">redisReply *reply;</div><div class="line">reply = redisCommand(c, <span class="string">"AUTH password"</span>);</div><div class="line">freeReplyObject(reply);</div><div class="line"></div><div class="line">...</div><div class="line"></div><div class="line">redisFree(c);</div></pre></td></tr></table></figure>
<p><strong>3.使用SSL和Redis</strong><br>hiredis不支持SSL本地连接。一个附加的安全方法，可以使用<a href="https://redislabs.com/blog/using-stunnel-to-secure-redis" target="_blank" rel="external">stunnel</a>。</p>
<p><strong>4.使用hiredis读写数据</strong><br>只要连接到redis数据库，就可以读数据和向redis数据：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// open a connection to Redis</span></div><div class="line">...</div><div class="line">redisReply *reply;</div><div class="line"></div><div class="line">reply = redisCommand(c,<span class="string">"SET %s %s"</span>,<span class="string">"foo"</span>,<span class="string">"bar"</span>);</div><div class="line">freeReplyObject(reply);</div><div class="line"></div><div class="line">reply = redisCommand(c,<span class="string">"GET %s"</span>,<span class="string">"foo"</span>);</div><div class="line"><span class="built_in">printf</span>(<span class="string">"%s\n"</span>,reply-&gt;str);</div><div class="line">freeReplyObject(reply);</div></pre></td></tr></table></figure>
<p>上面的代码输出应该是：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ gcc example_hiredis.c -o example_hiredis</div><div class="line">$ ./example_hiredis</div><div class="line">Connected to Redis</div><div class="line">bar</div></pre></td></tr></table></figure></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/redis/" rel="tag">#redis</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/10/10/redis_oper_abc/" rel="next" title="Redis 操作入门">
                <i class="fa fa-chevron-left"></i> Redis 操作入门
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/11/15/hbase_shell_abc/" rel="prev" title="Hbase shell基本操作">
                Hbase shell基本操作 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="LenhartG" />
          <p class="site-author-name" itemprop="name">LenhartG</p>
          <p class="site-description motion-element" itemprop="description">美国《时代周刊》2006年度风云人物</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">38</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">25</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/gxjares" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/2526255830" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#hiredis"><span class="nav-number">1.</span> <span class="nav-text">hiredis</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#C使用redis"><span class="nav-number">1.1.</span> <span class="nav-text">C使用redis</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数说明："><span class="nav-number">1.1.1.</span> <span class="nav-text">函数说明：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用步骤"><span class="nav-number">1.1.2.</span> <span class="nav-text">使用步骤</span></a></li></ol></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div>
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="http://music.163.com/outchain/player?type=2&id=208889&auto=1&height=66"></iframe>
</div>
<div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LenhartG</span>
</div>

<!--
<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>
-->

<!--
<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>
-->
        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  



  




  
  

  

  

  

  


</body>
</html>
